package cn.sinata.wanglin.property.activity;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.xilada.xldutils.activitys.RecyclerListActivity;
import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import java.util.ArrayList;

import cn.sinata.wanglin.MyApplication;
import cn.sinata.wanglin.adapter.RepairRecordAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.RepairRecord;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

public class RepairRecordsActivity extends RecyclerListActivity {

    private ArrayList<RepairRecord> mRepairRecords = new ArrayList<>();
    private RepairRecordAdapter repairRecordAdapter;
    private int pageNo = 0;
    private String userId;
    private String housingId;

    @Override
    protected void initView() {
        super.initView();
        setTitle("报修记录");

        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
        housingId = MyApplication.houseid;
        showDialog();
        getData();
    }

    @Override
    protected RecyclerView.Adapter setAdapter() {
        repairRecordAdapter = new RepairRecordAdapter(mRepairRecords);
        repairRecordAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                ActivityUtil.create(mContext)
                        .go(RepairRecordsDetailActivity.class)
                        .put("id",mRepairRecords.get(position).getId())
                        .start();
            }
        });
        return repairRecordAdapter;
    }

    void getData(){
        HttpManager.getRepair(housingId,userId,pageNo)
                .subscribe(new ResultDataSubscriber<ArrayList<RepairRecord>>(this) {
                    @Override
                    public void onSuccess(String msg, ArrayList<RepairRecord> repairRecords) {
                        if (pageNo == 0) {
                            mRepairRecords.clear();
                        }
                        if (repairRecords.size() == 0) {
                            if (pageNo == 0) {
                                setLoadMoreText("没有数据");
                            } else {
                                pageNo -- ;
                                setLoadMoreText("没有更多了");
                            }
                        }
                        mRepairRecords.addAll(repairRecords);
                        repairRecordAdapter.notifyDataSetChanged();
                    }
                });
    }

    @Override
    protected void pullDownRefresh() {
        super.pullDownRefresh();
        pageNo = 0;
        getData();
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        pageNo ++;
        getData();
    }
}
